/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import tools.MessageManager;

/**
 *
 * @author Mattia
 */
public class LicenseDialog extends javax.swing.JDialog {
    private boolean goon = false;
    public boolean isAccepted() {
        return goon;
    }
    /**
     * Creates new form LicenseDialog
     */
    public LicenseDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Layer = new javax.swing.JLayeredPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        scroll = new javax.swing.JScrollPane();
        text = new javax.swing.JTextPane();
        BTN_GoOn = new javax.swing.JButton();
        BTN_close = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/image/Logo_License.png"))); // NOI18N
        jLabel1.setBounds(0, 0, 300, 150);
        Layer.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Chromium terms of Use");
        jLabel2.setBounds(160, 90, 260, 40);
        Layer.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        scroll.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        text.setEditable(false);
        text.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        scroll.setViewportView(text);
        try {
            text.setText(this.readFromFile("ChromiumRedistLicense.txt"));
        } catch(IOException e) {
            MessageManager.warning(this, "Could not open Chromium License", e.getClass().toString());
        }

        scroll.setBounds(10, 140, 410, 360);
        Layer.add(scroll, javax.swing.JLayeredPane.DEFAULT_LAYER);

        BTN_GoOn.setText("Download Chromium");
        BTN_GoOn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_GoOnActionPerformed(evt);
            }
        });
        BTN_GoOn.setBounds(220, 510, 200, 23);
        Layer.add(BTN_GoOn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        BTN_close.setText("Exit");
        BTN_close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_closeActionPerformed(evt);
            }
        });
        BTN_close.setBounds(10, 510, 200, 23);
        Layer.add(BTN_close, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(Layer, javax.swing.GroupLayout.PREFERRED_SIZE, 431, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(Layer, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BTN_GoOnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_GoOnActionPerformed
        goon = true;
        dispose();
    }//GEN-LAST:event_BTN_GoOnActionPerformed

    private void BTN_closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_closeActionPerformed
        dispose();
    }//GEN-LAST:event_BTN_closeActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LicenseDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LicenseDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LicenseDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LicenseDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                LicenseDialog dialog = new LicenseDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BTN_GoOn;
    private javax.swing.JButton BTN_close;
    private javax.swing.JLayeredPane Layer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane scroll;
    private javax.swing.JTextPane text;
    // End of variables declaration//GEN-END:variables

    private String readFromFile(String strFile) throws IOException {
       FileInputStream stream = new FileInputStream(new File(strFile));
        try {
          FileChannel fc = stream.getChannel();
          MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
          /* Instead of using default, pass in a decoder. */
          return Charset.defaultCharset().decode(bb).toString();
        }
        finally {
          stream.close();
        }
    }
}
